<?php
require_once 'BaseController.php';

/**
 * 【SP版】建物詳細 コントローラ
 */
class Mobileapi_ArticleController extends Mobileapi_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $typ = $params['Type'];
        $code = $params['Code'];

        $this->makeTemplateParams($code);

        if (empty($this->_data['buken']['T03_HEYA_CD']))
        {
            error_log('no bukken');
            $this->my_redirect('sp/404/');

            /*
             * $this->getResponse()->setHttpResponseCode(404); $this->_404_header = true;
             * header("HTTP/1.1 404 Not Found"); Apm_Util::outputXmlHttpHeader404();
             * Apm_Util::outputCMSData404($html,$this->_property);
             */
            return;
        }

        $this->displayBody2($typ);
    }

    private function makeTemplateParams($code)
    {
        $model = new Rstdtl_Model_Articledtl();
        $model_conv = new Rstobi_Model_Converte();
        $model_shop = new Mobileapi_Model_BukkenTenpo();
        $modelLst = new Mobileapi_Model_BukkenList();

        $config = array();
        $params = array();

        $params['ID'] = $code;
        $params['buken_static'] = '1';
        $params['TatemonoCd'] = $code;

        $data = $model->getArticleDetail($code);
        // 気に入れる建物コード->再度代表建物取得
        if (!isset($data) || sizeof($data) == 0)
        {
            $daihyo = $model->getArticleDetail2($code);
            $code = $daihyo[0]['T03_DAIHYO_TATEMONO_CD'];
            $params['ID'] = $code;
            $params['buken_static'] = '1';
            $params['TatemonoCd'] = $code;

            $data = $model->getArticleDetail($code);
        }
        // 気に入れる建物コード->再度代表建物取得

        $model_conv->dataConverteForBuken($data, $params, false, false);
        $model->convertData($data, $params);

        // OEM判定
        $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));

        for ($i = 0; $i < sizeof($data); $i++)
        {
            $data[$i]['MAX_CHINRYO_STR'] = (intval($data[$i]['MAX_CHINRYO']) / 10000);
            $data[$i]['MIN_CHINRYO_STR'] = (intval($data[$i]['MIN_CHINRYO']) / 10000);
            // 物件画像取得
            $data[$i]['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($data[$i], $OemFlg);
            $data[$i]['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon($data[$i]['BUKEN_IMAGE']);
            // キャンペーン取得
            $heyaCd = $data[$i]['T03_HEYA_CD'];
            $ret = $modelLst->getCamp($heyaCd);
            if (!empty($ret))
            {
                $data[$i]['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                $data[$i]['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                $data[$i]['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
            }

            if (!isset($daihyobuken) && $data[$i]['T03_TATEMONO_CD'] == $data[$i]['T03_DAIHYO_TATEMONO_CD'])
            {
                $daihyobuken = $data[$i];
            }
        }

        // 都道府県
        if (!isset($daihyobuken)) $daihyobuken = $data[0];
        $daihyobuken["T03_PREF_NAME"] = Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$daihyobuken["T03_TODOUFUKEN_CD"]];
        $daihyobuken["T03_PREF_NAME_JP"] = Apm_Config_Shop::$PREF_LIST[$daihyobuken["T03_TODOUFUKEN_CD"]];

        $pref = $daihyobuken["T03_PREF_NAME_JP"];
        $city = $daihyobuken["T03_SHIKUCHOUSON_NAME_JP"];
        $address = $daihyobuken["T03_JYUUSHO_TEXT"];
        $daihyobuken["T03_CHOU_MEI"] = str_replace($city, "", str_replace($pref, "", $address));
        // 都道府県

        $tenpo1 = $model_shop->getTatemonoTenpoList($code);
        if (count($tenpo1) == 0)
        {
            $tenpo1 = $model_shop->getTenpoListTatemono($code);
        }
        $daihyobuken['tenpo1'] = $tenpo1;
        // CALLNOTE
        foreach ($daihyobuken['tenpo1'] as $i => $tenpo)
        {
            $tempData = Apm_ShopUtil::getCallingData($tenpo, Apm_Config_Shop::FREE_CONNECTION_NO_6);
            $daihyobuken['tenpo1'][$i]['IS_FREE_CALL'] = $tempData['IS_FREE_CALL'];
            $daihyobuken['tenpo1'][$i]['hasFreeTel'] = $tempData['hasFreeTel'];
            if ($tempData['IS_FREE_CALL'])
            {
                $daihyobuken['tenpo1'][$i]['FREE_CALLING'] = $tempData['TEL_01'];
                $daihyobuken['tenpo1'][$i]['CONVERT_CONNECTION_NO'] = Apm_Bukkenitems::GetBukkenWebCallingNo($tenpo, Apm_Config_Rstdtl::CAMPAIGN_PC);
            }
        }

        // Callクレヨン対応
        $smodel = new Shop_Model_Detail();
        foreach ($daihyobuken['tenpo1'] as $i => $tenpo)
        {
            $callCrayonNumber = $smodel->getCallCrayonNumber($this->_config['callcrayons']['api'], $this->_config['callcrayons']['service_id'], $daihyobuken['tenpo1'][$i]['T03_TENPO_CD'], '12');
            $daihyobuken['tenpo1'][$i]['CALL_CRAYON_NO'] = $callCrayonNumber;
            /*
             * if (! empty ( $daihyobuken['tenpo1'][$i] ['CALL_CRAYON_NO'] )) { // フリー番号ではない・・・
             * $daihyobuken['tenpo1'][$i] ['hasFreeTel'] = false; $daihyobuken['tenpo1'][$i]
             * ['IS_FREE_CALL'] = false; }
             */
        }

        $this->_data['buken'] = $daihyobuken;
        $this->_data['bukenlist'] = $data;
        $this->_footer_data['ftBuken'] = $daihyobuken;
        $this->getAreaData();
    }

    private function getAreaData()
    {
        $prefCd = $this->_data['buken']['T03_TODOUFUKEN_CD'];
        $cityCd = $prefCd . $this->_data['buken']['T03_SHIKUCHOUSON_CD'];

        $model = new Search_Model_Selareasearch();
        $this->_data['PrefInfo'] = $model->getSimpleArea($prefCd);

        $this->_data['PrefName'] = $this->getPrefName($prefCd);
        $this->_data['PrefNameJP'] = $this->getPrefNameJP($prefCd);

        $selcyoModel = new Search_Model_Selcyo(); // 市区町村
        $params = array();
        $params['PrefCd'] = array(
            $prefCd
        );
        $params['CityCd'] = array(
            $cityCd
        );
        $shiku = $selcyoModel->getShikucyosonNameByShikucyosonCd($params);
        $this->_data['ShikuName'] = $shiku[0]['M01_SHIKUCHOUSON_ALIAS'];
        $this->_data['ShikuCd'] = substr($cityCd, 3, 3);
    }

}
?>